import * as React from 'react';
import * as ReactDOM from 'react-dom';
import * as Reflux from 'reflux';


import * as $ from 'jquery';

var main = function() {
    var CommonActions = Reflux.createActions([
        'get', 'post'
    ]);
    var CommonStore = Reflux.createStore({
        items: {},
        listenables: [CommonActions],
        onGet: function(url, model, param) {
            $.get(url, model, function(data) {
                if (typeof(data) == 'string') {
                    data = JSON.parse(data);
                }
                if (data.resultCode == "0") {
                    this.items = data;
                    this.trigger(this.items, param);
                } else {
                    this.items = data;
                    this.trigger(this.items, param);
                }
                //console.log(data);
                return false;

            }.bind(this));
        },
        onPost: function(url, model, param) {

            $.post(url, model, function(data) {
                if (typeof(data) == 'string') {
                    data = JSON.parse(data);
                }

                if (data.resultCode == "0") {
                    this.items = data;
                    this.trigger(this.items, param);
                } else {
                    this.items = data;
                    this.trigger(this.items, param);
                }
                //console.log(data);
                return false;

            }.bind(this));
        }
    });
    return {
        CommonStore: CommonStore,
        CommonActions: CommonActions
    }
}

var init = function() {
    var temp = new main();
    return {
        CommonStore: temp.CommonStore,
        CommonActions: temp.CommonActions
    }
}

module.exports = {
    init: init
};